package com.cl.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.cl.db.dao.AntivirusDao;
import com.cl.safe.R;
import com.cl.utils.MD5Utils;

public class AntiVirusActivity extends Activity {
	protected static final int SCANING = 1;
	protected static final int SCAN_FINISH = 2;
	private ImageView iv_scan;
	private TextView tv_scan_status;
	private ProgressBar progressBar1;
	private LinearLayout ll_container;
	/**
	 * 病毒查询的集合
	 */
	private List<ScanInfo> virusInfos;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SCANING://正在扫描
				ScanInfo scanInfo = (ScanInfo) msg.obj;
				tv_scan_status.setText("正在扫描： "+scanInfo.appname);
				TextView tv = new TextView(getApplicationContext());
				if (scanInfo.isVirus) {
					tv.setText("发现病毒：" + scanInfo.appname);
					tv.setTextColor(Color.RED);
				} else {
					tv.setText("扫描安全：" + scanInfo.appname);
					tv.setTextColor(Color.BLACK);
				}
				ll_container.addView(tv, 0);
				break;
			case SCAN_FINISH://扫描完成
				tv_scan_status.setText("扫描完成");
				iv_scan.clearAnimation();
				if(virusInfos.size() > 0){//发现了病毒
					AlertDialog.Builder builder = new Builder(AntiVirusActivity.this);
					builder.setTitle("警告!!!");
					builder.setMessage("在您的手机里面发现了："+virusInfos.size()+"个病毒,手机已经十分危险，得分0分，赶紧查杀！！！");
					builder.setNegativeButton("下次再说", null);
					builder.setPositiveButton("立刻处理", new OnClickListener() { 
						@Override
						public void onClick(DialogInterface dialog, int which) {
							for(ScanInfo info: virusInfos){
								Intent intent = new Intent(); 
								intent.setAction(Intent.ACTION_DELETE);
								intent.setData(Uri.parse("package:"+info.packName));
								startActivity(intent);
							}
						}
					});
					builder.show();
				}else{
					Toast.makeText(getApplicationContext(), "您的手机非常安全，继续加油哦", 0).show();
				}
				break;
			}
		};
	};
	private PackageManager pm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_anti_virus);
		virusInfos = new ArrayList<AntiVirusActivity.ScanInfo>();
		iv_scan = (ImageView) findViewById(R.id.iv_scan);
		RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
				 Animation.RELATIVE_TO_SELF, 0.5f);
		ra.setDuration(1000);
		ra.setRepeatCount(Animation.INFINITE);
		iv_scan.startAnimation(ra);
		
		progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
		ll_container  = (LinearLayout) findViewById(R.id.ll_container);
		tv_scan_status = (TextView) findViewById(R.id.tv_scan_status);
		tv_scan_status.setText("正在初始化8核杀毒引擎");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//扫描apk
		new Thread(){
			public void run() {
				// 获取应用程序的特征码。
				pm = getPackageManager();
				List<PackageInfo> packageInfos = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES
						+ PackageManager.GET_SIGNATURES);
				progressBar1.setMax(packageInfos.size());
				int progress = 0;
				for(PackageInfo packinfo : packageInfos) {
					ScanInfo scanInfo = new ScanInfo();
					scanInfo.packName = packinfo.packageName;
					scanInfo.appname = packinfo.applicationInfo.loadLabel(pm).toString();
					String md5 = MD5Utils.md5Password(packinfo.signatures[0]
							.toCharsString());
					System.out.println(md5+" :packName: "+scanInfo.appname);
					String result = AntivirusDao.isVirus(md5);
					if(result != null) {
						//发现病毒
						System.out.println("发现病毒" + result);
						scanInfo.desc = result;
						scanInfo.isVirus = true;
						virusInfos.add(scanInfo);
					}else {
						//没有病毒
						System.out.println("扫描安全");
						scanInfo.desc = result;
						scanInfo.isVirus = false;
						
					}
					progress ++;
					progressBar1.setProgress(progress);
					Message msg = Message.obtain();
					msg.what = SCANING;
					msg.obj = scanInfo;
					handler.sendMessage(msg);
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				Message msg = Message.obtain();
				msg.what = SCAN_FINISH;
				handler.sendMessage(msg);
			};
		}.start();
	}
	
	class ScanInfo {
		boolean isVirus;
		String packName;
		String desc;
		String appname;
	}
}
